package thread;

public class SyncDemo2 {
    public static void main(String[] args) {
        Shopping shop=new Shopping();
            Thread t1=new Thread("Wong"){
                @Override
                public void run() {
                    shop.buy();
                }
            };
            Thread t2=new Thread("Fan"){
                @Override
                public void run() {
                    shop.buy();
                }
            };
        t1.start();
        t2.start();
    }
}
class Shopping {
//    public synchronized void buy(){
    public void buy(){
        try {
            Thread t=Thread.currentThread();

            System.out.println(t.getName()+" 正在挑衣服..");
            Thread.sleep(3000);
            synchronized(this){
                System.out.println(t.getName() + " 正在试衣间试衣服..");
                Thread.sleep(3000);
            }
            System.out.println(t.getName()+" 结账走人！");
        } catch (InterruptedException e) {

        }
    }
}